package com.inforich.appManage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.inforich.appManage.domain.AppUser;
import com.inforich.appManage.service.IAppRoleService;
import com.inforich.appManage.service.IAppUserService;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.RoleCodeEnum;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.appManage.mapper.ContactMapper;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IContactService;

/**
 * 联系人管理Service业务层处理
 *
 * @author hjm
 * @date 2022-10-27
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements
    IContactService {

  @Autowired
  private IAppRoleService appRoleService;
  @Autowired
  private IAppUserService appUserService;

  @Override
  public Page<Contact> pageContact(Page<Contact> page, Map<String, Object> map) {
    return baseMapper.pageContact(page, map);
  }

  @Override
  public List<Contact> listContact(Map<String, Object> map) {
    return baseMapper.listContact(map);
  }

  @Override
  public AjaxResult updateContact(Contact contact) {
    // 角色编码
    String appRoleId = contact.getAppRoleId();
    String roleCode = appRoleService.getRoleCodeById(appRoleId);

    if (RoleCodeEnum.PROJECT_ADMIN.getRoleCode().equals(roleCode)) {
      if (StrUtil.isBlank(contact.getProjectId())) {
        return AjaxResult.error("项目管理员请指定项目");
      }
      contact.setDriverId(null);
      contact.setTransportCompanyId(null);
    }
    if (RoleCodeEnum.TRANSPORT_COMPANY_ADMIN.getRoleCode().equals(roleCode)) {
      if (StrUtil.isBlank(contact.getTransportCompanyId())) {
        return AjaxResult.error("清运公司负责人请指定清运公司");
      }
      contact.setDriverId(null);
      contact.setProjectId(null);
    }
    if (RoleCodeEnum.DRIVER.getRoleCode().equals(roleCode)) {
      if (StrUtil.isBlank(contact.getTransportCompanyId())) {
        return AjaxResult.error("司机请指定清运公司");
      }
      if (null == contact.getDriverId()) {
        return AjaxResult.error("司机请指定关联司机");
      }
      contact.setProjectId(null);
    }
    return AjaxResult.success(updateById(contact));
  }

  @Override
  public Contact getByAppUserId(String userId) {
    return this.getOne(new LambdaQueryWrapper<Contact>()
        .eq(Contact::getAppUserId, userId));
  }

  @Override
  public String getProjectIdByAppUserId(String appUserId) {
    Contact contact = getByAppUserId(appUserId);
    return contact == null ? "" : contact.getProjectId();
  }

  @Override
  public String getTransportCompanyIdByAppUserId(String appUserId) {
    Contact contact = getByAppUserId(appUserId);
    return contact == null ? "" : contact.getTransportCompanyId();
  }

  @Override
  public Long getDriverIdByAppUserId(String appUserId) {
    Contact contact = getByAppUserId(appUserId);
    return contact == null ? null : contact.getDriverId();
  }

  @Override
  public void addProject(String appUserId, String projectId) {
    Contact contact = createContactByAppUserId(appUserId);
    contact.setAppRoleId(appRoleService.getIdByRoleCode(RoleCodeEnum.PROJECT_ADMIN.getRoleCode()));
    contact.setProjectId(projectId);
    this.saveOrUpdate(contact);
  }

  @Override
  public void addCompany(String appUserId, String companyId) {
    Contact contact = createContactByAppUserId(appUserId);
    contact.setAppRoleId(
        appRoleService.getIdByRoleCode(RoleCodeEnum.TRANSPORT_COMPANY_ADMIN.getRoleCode()));
    contact.setTransportCompanyId(companyId);
    this.saveOrUpdate(contact);
  }

  @Override
  public void removeByAppUserId(String appUserId) {
    remove(new LambdaQueryWrapper<Contact>()
        .eq(Contact::getAppUserId, appUserId));
  }

  @Override
  public void removeByCompanyId(String companyId) {
    remove(new LambdaQueryWrapper<Contact>()
        .eq(Contact::getTransportCompanyId, companyId));
  }

  private Contact createContactByAppUserId(String appUserId) {
    AppUser appUser = appUserService.getById(appUserId);
    Contact contact = getByAppUserId(appUserId);
    if (contact == null) {
      contact = new Contact();
    }
    contact.setAppUserId(appUser.getId());
    contact.setContactName(appUser.getUserName());
    contact.setContactPhone(appUser.getUserPhone());
    return contact;
  }

}
